package com.yzqp.common.util;

import org.springframework.util.StringUtils;

import java.io.*;

/**
 * 文件工具类
 *
 * @author hejiacheng
 * @date 2021-12-1
 */
public class FileUtil {

    public static String projectRootPath = null;

    public static String readFile(String path) {
        StringBuffer res = new StringBuffer();
        try {
//            path = "E:\\myproject\\blog\\blog_server\\src\\main\\java\\com\\yzqp\\generate\\util\\FileUtil.java";
            FileReader fileReader = new FileReader(path);

            int len = 0;
            while ((len = fileReader.read()) != -1) {
                res.append((char) len);
            }
            return res.toString();

        } catch (FileNotFoundException e) {
            System.out.println("读取文件异常, 路径 : " + path);
            e.printStackTrace();
        } finally {
            return res.toString();
        }
    }

    /**
     * 读取模板
     *
     * @param path 模板路径
     * @return
     */
    public static String readTemple(String path) {
        StringBuffer res = new StringBuffer();
        try {
            FileReader fileReader = new FileReader(path);

            int len = 0;
            while ((len = fileReader.read()) != -1) {
                res.append((char) len);
            }

        } catch (FileNotFoundException e) {
            System.out.println("读取文件异常, 路径 : " + path);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("读取文件异常, 路径 : " + path);
            e.printStackTrace();
        }
        // 从"@startTemple"的下一行 开始读文件
        int index = -2;
        if ((index = res.indexOf("@startTemple")) > -1) {
            // 去掉@startTemple
            String resString = res.substring(index + 1);
            res = new StringBuffer(resString);
            // 去掉换行符
            if ((index = res.indexOf("\n")) > -1) {
                resString = res.substring(index + 1);
                res = new StringBuffer(resString);
            }
        }
        return res.toString();
    }


    /**
     * 判断是否存在文件
     *
     * @param path
     * @return
     */
    public static boolean exist(String path) {
        File file = new File(path);
        if (file.exists()) {//判断文件目录的存在
            return true;
        }
        return false;
    }


    /**
     * 创建文件
     *
     * @param path
     */
    public static File createFile(String path) {
        File file = new File(path);
        try {
            // 父文件
            File parentFile = file.getParentFile();
            // 不存在，就创建目录
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            // 创建新文件
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 写入文件
     *
     * @param path
     * @return
     */
    public static void writeFile(String path, String value) {
        // 写入数据
        BufferedWriter writer = null;
        try {
            // 声明一个文件
            File f = new File(path);
            // 获取父文件夹
            File parentDir = f.getParentFile();
            // 不存在文件夹，就创建文件夹
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }

            FileOutputStream writerStream = new FileOutputStream(path);
            writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
            writer.write(value);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取项目根路径
     * 即 返回 E:\\myproject\\blog\\blog_server
     *
     * @return
     */
    public static String getCurrentProjectRootPath() {
        if (StringUtils.isEmpty(FileUtil.projectRootPath)) {
            //当前项目下路径
            File file = new File("");
            String filePath = null;
            try {
                filePath = file.getCanonicalPath();
                FileUtil.projectRootPath = filePath;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return FileUtil.projectRootPath;
    }

    /**
     * 获得当前项目的饿java包路径
     * 即 返回 E:\\myproject\\blog\\blog_server\\src\\main\\java
     *
     * @return
     */
    public static String getCurrentProjectJavaPackageRootPath() {
        // 当前项目下路径
        String projectRootPath = FileUtil.getCurrentProjectRootPath();
        projectRootPath = projectRootPath + File.separator + "src";
        projectRootPath = projectRootPath + File.separator + "main";
        projectRootPath = projectRootPath + File.separator + "java";
        return projectRootPath;
    }


    public static void main(String[] args) throws Exception {
        System.out.println("读取开始....");
//        System.out.println(FileUtil.readTemple(""));
        System.out.println("读取结束....");

        // FileUtil.writeFile("E:\\myproject\\blog\\blog_server\\src\\main\\java\\com\\yzqp\\generator\\test\\bean\\user.java", "test write");
        // 获得当前项目根路径
        String path = FileUtil.getCurrentProjectRootPath();
        System.out.println(path);
    }

}
